proc sort data=sdtm.qs out=qs;
 by usubjid qsseq;
 *where qsstat="";
run;

 data suppqs(keep=usubjid qsseq qval rename=(qval=qsdate));  
 set sdtm.suppqs; 
  qsseq=input(idvarval, best.);  
 run;  

 proc sort data=suppqs; 
 by usubjid qsseq; 
 
 run; 

data qs;
 length avisit $200 parcat1 parcat2  $200 paramcd $8 param $40 avalc $200 atpt $200;
 merge qs (in=a) suppqs(in=b);
 by usubjid qsseq; 
     if a;

 avisit=strip(propcase(visit));   
 avisitn=visitnum;
 parcat1=strip(qscat);
 parcat2=strip(qsscat);
 paramcd=strip(qstestcd);   
 param=strip(propcase(qstest));
   
   if length(qsdtc) > 10 then do;
     adt=input(substr(qsdtc,1,10), yymmdd10.);
     atm=input(substr(qsdtc,12), is8601tm.);
     adtm=input(qsdtc, is8601dt.);
   end;
    else  if length(qsdtc) <= 10 then do;
     adt=input(substr(qsdtc,1,10), yymmdd10.);
     atm=.;
     adtm=.;
   end;
   format adt date9. atm time8. adtm datetime21.;
   

  /* if index(paramcd,"MINI") > 0 then do;
     paramn=input(substr(paramcd, anydigit(paramcd)), best.);
     parcat1n= 4;
   end;
   if index(paramcd,"ETH0") > 0 then do;
      if paramcd="ETH01" then paramn=41;
      if paramcd="ETH02" then paramn=42;
      if paramcd="ETH03" then paramn=43;
      if paramcd="ETH04" then paramn=44;
      if paramcd="ETH05" then paramn=45;
      if paramcd="ETH06" then paramn=46;
      parcat1n=1;
   end;
   if index(paramcd,"MDEQ") > 0 then do;
      if paramcd="MDEQ01" then paramn=51;
      if paramcd="MDEQ02" then paramn=52;
      if paramcd="MDEQ03" then paramn=53;
      if paramcd="MDEQ04" then paramn=54;
      if paramcd="MDEQ05" then paramn=55;
      parcat1n=3;
   end;
	if paramcd = "FLUSHING" then do;
      paramn=61;
      parcat1n=2;
   end;*/
             

       
   aval=qsstresn;
   avalc=qsstresc;
   atpt = strip(qstpt);
   atptn = qstptnum;
   
   
   drop qstest qstestcd qsstresn qsorres qsstresc qstpt qstptnum ;
 run;
 proc sort data=adam.adsl out=adsl;
  by usubjid;
 run;
 proc sort data= qs;
  by usubjid;
 run;
 
 data qs1;
  merge qs(in=a) adsl(in=b keep=usubjid trtsdt trtsdtm);
  by usubjid;
  if a ;
 run;

 proc sort data=qs1 out=base1;
  by usubjid parcat1 parcat2 paramcd avisitn avisit atptn atpt;
  where (aval ne . or avalc ne " ") and qsblfl = 'Y' ;
 run; 
 
 data base2(keep=usubjid paramcd parcat1 parcat2 avisitn avisit atptn atpt ablfl) 
      base3(keep=usubjid paramcd parcat1 parcat2 avisitn avisit base basec );
   length ablfl $1 basec $50 ;
  set base1;
  by usubjid parcat1 parcat2 paramcd avisitn avisit atptn atpt;
  ablfl="Y";
  base=aval;
  basec=avalc;
  run;
  
 proc sort data= qs1;
  by usubjid parcat1 parcat2 paramcd avisitn avisit atptn atpt;
  run;

 data qs2;
  merge qs1(in=a) base2(in=b);
  by usubjid parcat1 parcat2 paramcd avisitn avisit atptn atpt;
  if a;
 run; 

 proc sort data= qs2;
  by usubjid parcat1 parcat2 paramcd avisitn avisit  ;
  run;

 data qs3;
   merge qs2(in=a) base3(in=b);
   by usubjid parcat1 parcat2 paramcd avisitn avisit;
   if a;
   if nmiss(aval, base)=0 then chg=aval-base;
    if adt ne . and trtsdt ne . then do;
    if adt< trtsdt then ady=adt-trtsdt;
   else if adt >= trtsdt then ady=adt-trtsdt+1;
	end;
  run;
  
  proc sort data=qs3;
  by usubjid;
  run;

  data qs4;
   length trtp trta $100 trtpn trtan 8.;
   merge qs3(in=a drop=trtsdtm) adsl(in=b);
   by usubjid;
   if a;
   if visit='Day 1' then do;
   trtp=trt01p;
   trta=trt01a;
   trtpn=trt01pn;
	trtan=trt01an;
end;
if visit='Day 2' then do;
   trtp=trt02p;
   trta=trt02a;
   trtpn=trt02pn;
	trtan=trt02an;
end;
if visit='Day 3' then do;
   trtp=trt03p;
   trta=trt03a;
   trtpn=trt03pn;
	trtan=trt03an;
end;
if visit='Day 4' then do;
   trtp=trt04p;
   trta=trt04a;
   trtpn=trt04pn;
	trtan=trt04an;
end;
if visit='Day 5' then do;
   trtp=trt05p;
   trta=trt05a;
   trtpn=trt05pn;
	trtan=trt05an;
end;
if visit='Day 6' then do;
   trtp=trt06p;
   trta=trt06a;
   trtpn=trt06pn;
	trtan=trt06an;
end;
  


     if index(avisit, "Unsch") =0 then anl01fl="Y";
     
     
   keep STUDYID USUBJID SITEID SUBJID ARM ARMCD ACTARM ACTARMCD ENRLFL SAFFL PKFL PDFL 
   /*PKPDFL*/ RANDAFL RANDBFL COMPLFL 
        TRTP TRTPN TRTA TRTAN TRT01P TRT01PN TRT01A TRT01AN TRTSDTM TRTEDTM 
        QSSEQ VISIT VISITNUM AVISIT AVISITN ADT ATM ADTM ADY QSDTC PARCAT1 PARCAT2 /*PARCAT1N*/ PARAMCD PARAM /*PARAMN*/  AVAL AVALC 
        ABLFL BASE BASEC CHG ANL01FL ATPT ATPTN ;

   RUN;
   data qs5;
     attrib 
      STUDYID        label = "Study Identifier "                               length =  $10.
      USUBJID        label = "Unique Subject Identifier "                      length =  $17.
      SUBJID         label = "Subject Identifier for the Study "               length = $6.
      SITEID         label = "Study Site Identifier "                          length =  $2.
      RANDAFL         label = "Randomized Population Flag in Part A"                     length = $1.
      RANDBFL         label = "Randomized Population Flag in Part B"                     length = $1.
      SAFFL          label = "Safety Population Flag "                         length =  $1.
      PKFL           label = "Pharmaco-Kinetic Population Flag"                length =  $1.
      PDFL           label = "Pharmaco-Dynamic Population Flag"                length = $1.
/*      PKPDFL         label = "PharmacoKinetic/PharmacoDynamic Population Flag" length = $1.*/
      COMPLFL        label = "Completers Population Flag"                      length = $1. 
      TRTP           label = "Planned Treatment"                               length =  $100.
	  TRTPN          label = "Planned Treatment (N)"                           length =  8.
      TRTA           label = "Actual Treatment"                                length =  $100.
	  TRTAN          label = "Actual Treatment (N)"                            length =  8.
	  TRT01P         label = "Planned Treatment for Period 01"                 length = $100.
      TRT01PN        label = "Planned Treatment for Period 01 (N)"             length = 8.
      TRT01A         label = "Actual Treatment for Period 01"                  length = $100.
      TRT01AN        label = "Actual Treatment for Period 01 (N)"              length = 8.
      TRTSDTM        label = "Datetime of First Exposure to Treatment "        length =  8
      TRTEDTM        label = "Datetime of Last Exposure to Treatment "         length =  8
      QSSEQ          label = "Sequence Number"                                 length =  8.
      VISIT          label = "Visit Name"                                      length =  $100.
      VISITNUM       label = "Visit Number"                                    length =  8
      AVISIT         label = "Analysis Visit"                                  length =  $200
      AVISITN        label = "Analysis Visit (N)"                              length =  8
      ADT            label = "Analysis Date"                                   length =  8
      ATM            label = "Analysis Time"                                   length =  8
      ADTM           label = "Analysis Datetime"                              length =  8
      ADY            label = "Analysis Relative Day"                           length =  8.
      QSDTC          label = "Date/Time of Finding"                            length =  $20.
      PARCAT1        label = "Parameter Category 1"                            length =  $200.
      PARCAT2        label = "Parameter Category 2"                            length =  $200.
/*      PARCAT1N       label = "Parameter Category 1 (N)"                        length =  8*/
      PARAMCD        label = "Parameter Code"                                  length =  $8
      PARAM          label = "Parameter"                                       length =  $40.
/*      PARAMN         label = "Parameter (N)"                                   length =  8. */
      AVAL           label = "Analysis Value"                                  length =  8
      AVALC          label = "Analysis Value (C)"                              length =  $200.
      ABLFL          label = "Baseline Record Flag"                            length =  $1.
      BASE           label = "Baseline Value"                                  length =  8.
      BASEC          label = "Baseline Value (C)"                              length =  $50.
      CHG            label = "Change from Baseline"                            length =  8.
      ANL01FL        label = "Analysis Flag 01"                         length =  $1.
      ATPT           label = "Analysis Timepoint"                              length =  $200
      ATPTN          label = "Analysis Timepoint (N)"                          length =  8.
;
    set qs4;
/*if avisitn in (1,7) or (avisitn in (2) and atptn in (0,1)) then do;
   	trta="";
   	trtan=.;
 end;*/
 run;   
   
   proc sort data=qs5;
    by usubjid parcat1 parcat2  param paramcd avisitn atptn;
    *where avisit ^= "Baseline";
   run; 
   
 Proc SQL;   
create table adamw.adqs(label = "Questionnaires Analysis Dataset" sortedby =STUDYID USUBJID parcat1 parcat2 PARAMCD compress=yes ) as
   SELECT 
   STUDYID, USUBJID, SITEID, SUBJID, ARM, ARMCD, ACTARM, ACTARMCD, ENRLFL, SAFFL, PKFL, PDFL, 
   /*PKPDFL*/ RANDAFL,RANDBFL, COMPLFL, 
        TRTP,TRTPN, TRTA,TRTAN,/*TRT01P, TRT01PN, TRT01A, TRT01AN,*/ TRTSDTM, TRTEDTM, 
        QSSEQ, VISIT, VISITNUM, AVISIT, AVISITN, ADT, ATM, ADTM, ADY, /*QSDTC,*/ PARCAT1,/*PARCAT1N ,*/ PARCAT2, PARAMCD, /*PARAMN,*/ PARAM , AVAL ,AVALC, 
        ABLFL, BASE, BASEC, CHG, ANL01FL, ATPT, ATPTN 
   from qs5
   order by STUDYID, USUBJID,PARCAT1, PARCAT2, PARAMCD, AVISITN, ATPTN
   ;
QUIT;


/* Proc SQL;*/
/*create table adamw.adqs(label = "Questionnaires Analysis Dataset" sortedby =STUDYID USUBJID parcat1 parcat2 PARAMCD compress=yes ) as*/
/*   SELECT STUDYID, USUBJID, SITEID, SUBJID, SAFFL, PKFL, PDFL, */
/*RANDAFL,RANDBFL, COMPLFL, */
/*		TRTP,TRTPN, TRTA,TRTAN, TRTSDTM, TRTEDTM, */
/*        QSSEQ, VISIT, VISITNUM, AVISIT, AVISITN, ADT, ATM, ADTM, ADY, PARCAT1, PARCAT2, PARAMCD, PARAM , AVAL ,AVALC, */
/*        ABLFL, BASE, BASEC, CHG, ANL01FL, ATPT, ATPTN */
/*   from qs5*/
/*   order by STUDYID, USUBJID,PARCAT1, PARCAT2, PARAMCD, AVISITN, ATPTN*/
/*   ;*/
/*QUIT;*/
 
 proc contents data=adam.adqs; 
run;
 
%clrw;
 
  
 
    
   
   
 
